package com.cxg.fairy.ec.sign;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cxg.fairy.app.AccountManager;
import com.cxg.fairy.ec.database.DatabaseManager;
import com.cxg.fairy.ec.database.UserProfile;

/**
 * Created by Administrator on 2017/11/8.
 */

public class SignHandler {

    public static void onSignIn(String response, ISignListener signListener) {

        final JSONArray profileJsonArr =  JSON.parseArray(response);

        JSONObject profileJsonObji =  profileJsonArr.getJSONObject(2);

        final long userId = profileJsonObji.getLong("userId");;
        final String name = profileJsonObji.getString("name");
        final String account = profileJsonObji.getString("account");
        final String password = profileJsonObji.getString("email");
        final String avatar = "avatar";
        final String gender = "gender";
        final String address = profileJsonObji.getString("fact");
        final UserProfile profilei = new UserProfile(userId, name, account, password, avatar, gender, address);
        DatabaseManager.getInstance().getDao().insertOrReplace(profilei);
        //已经注册并登录成功了
        AccountManager.setSignState(true);
        signListener.onSignInSuccess();
    }


    public static void onSignUp(String response, ISignListener signListener) {

        final JSONArray profileJsonArr =  JSON.parseArray(response);

        JSONObject profileJsonObj =  profileJsonArr.getJSONObject(1);
        final long userId = profileJsonObj.getLong("userId");;
        final String name = profileJsonObj.getString("name");
        final String account = profileJsonObj.getString("account");
        final String password = profileJsonObj.getString("email");
        final String avatar = "avatar";
        final String gender = "gender";
        final String address = profileJsonObj.getString("fact");
        final UserProfile profile = new UserProfile(userId, name, account, password, avatar, gender, address);
        DatabaseManager.getInstance().getDao().insertOrReplace(profile);

        //已经注册并登录成功了
        AccountManager.setSignState(true);
        signListener.onSignUpSuccess();
    }
}
